package com.offcn.pay.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 对应队列以及对应交换机绑定
 *
 * @author 秋林
 * @date 2022/10/20 20:39:54
 */
@Component
public class bind {
    @Autowired
    private Environment environment;

    /**
     * 创建队列
     *
     * @return {@link Queue}
     */
    public Queue createQueue() {
        return new Queue(environment.getProperty("mq.pay.queue.order"));
    }

    /**
     * 创建交换机
     *
     * @return {@link DirectExchange}
     */
    public DirectExchange basicExchanage() {
        return new DirectExchange(environment.getProperty("mq.pay.exchange.order"));
    }

    /**
     * 绑定
     *
     * @return {@link Binding}
     */
    public Binding basicBinding() {
        return BindingBuilder.bind(createQueue()).to(basicExchanage()).with(environment.getProperty("mq.pay.routing.key"));
    }

    /**
     * 创建秒杀队列
     *
     * @return {@link Queue}
     */
    @Bean(name="seckillQueue")
    public Queue createSeckillQueue() {
        return new Queue(environment.getProperty("mq.pay.queue.seckillorder"));
    }

    /**
     * 创建秒杀交换机
     *
     * @return {@link DirectExchange}
     */
    @Bean(name="seckillExchanage")
    public DirectExchange basicSeckillExchanage(){
        return new DirectExchange(environment.getProperty("mq.pay.exchange.seckillorder"));
    }

    @Bean(name="SeckillBinding")
    public Binding basicSeckillBinding(){
        return  BindingBuilder.bind(createSeckillQueue()).to(basicSeckillExchanage()).with(environment.getProperty("mq.pay.routing.seckillkey"));
    }

}
